/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2015年3月18日         luwei
 */

package com.coocaa.app.gamecenter.pages.mygame;

import android.content.ComponentName;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.RemoteException;
import android.view.Gravity;
import android.view.View;
import android.widget.AbsListView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.coocaa.launcher.framework.sidebar.item.Item;
import com.coocaa.launcher.framework.sidebar.item.ItemView;
import com.coocaa.x.service.XServiceManager;
import com.coocaa.x.service.litecache.data.client.LiteImageCache.LiteImageCacheLoadListener;
import com.coocaa.x.service.litecache.data.client.LiteImageCache.LiteImageCacheObject;
import com.tianci.appstore.R;
import com.tianci.appstore.ui.data.SkyListAppData;
import com.tianci.appstore.ui.widget.AlwaysMarqueeTextView;
import com.tianci.appstore.ui.widget.CatchImageView;
import com.tianci.appstore.utils.SkyConstants;

public class MyGameItem extends Item implements LiteImageCacheLoadListener {
    private SkyListAppData mAppData = null;
    private LiteImageCacheObject mImageObj = null;

    public MyGameItem(Context context) {
        super(context);
        AbsListView.LayoutParams params = new AbsListView.LayoutParams(SkyConstants.Div(282),
                SkyConstants.Div(282));
        mItemView.setLayoutParams(params);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void setData(Object object) {
        mAppData = (SkyListAppData) object;
        ((MyGameView) mItemView).updateName(mAppData.getTitle());
        try {
            PackageInfo packageInfo = mContext.getPackageManager().getPackageInfo(
                    mAppData.getPkgName(), 0);
            ((MyGameView) mItemView).updateIcon(packageInfo.applicationInfo.loadIcon(mContext
                    .getPackageManager()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        // if(!AppIconCache.getAppIcon(mAppData.getPkgName(), SkyConstants.Div(145),
        // SkyConstants.Div(145), this))
        // {
        //
        // }
    }

    @Override
    public void onItemClick(View v) {
        try {
            XServiceManager.getLitePMService().startAppByComponentName(mContext.getClass().getName(), false,
                    new ComponentName(mAppData.getPkgName(), mAppData.getMainActivity()));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        super.onItemClick(v);
    }

    @Override
    public void destory() {
        mItemView.destory();
    }

    public class MyGameView extends ItemView {
        private ImageView mIcon = null;
        private TextView mName = null;

        public MyGameView(Context context) {
            super(context);
            addIcon();
            addName();
            enableFocusable(false);
        }

        private void addIcon() {
            mIcon = new CatchImageView(mContext);
            mIcon.setBackgroundResource(R.drawable.game_center_icon_default);
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(Div(145), Div(145),
                    Gravity.CENTER_HORIZONTAL);
            params.topMargin = Div(45);
            addView(mIcon, params);
        }

        private void addName() {
            mName = new AlwaysMarqueeTextView(mContext);
            mName.setTextColor(Color.WHITE);
            mName.setTextSize(Dpi(30));
            mName.setGravity(Gravity.CENTER);
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(Div(245),
                    LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
            params.bottomMargin = Div(42);
            addView(mName, params);
        }

        public void updateName(String name) {
            mName.setText(name);
        }

        public void updateIcon(Drawable drawable) {
            mIcon.setBackgroundDrawable(drawable);
        }

        @Override
        public void destory() {
            Drawable drawable = mIcon.getBackground();
            if (drawable != null) {
                drawable.setCallback(null);
            }
            mIcon.setBackgroundDrawable(null);
            if (mImageObj != null) {
                mImageObj.recyle();
            }
        }

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            mName.setSelected(hasFocus);
            super.onFocusChange(v, hasFocus);
        }
    }

    @Override
    public ItemView createItemView() {
        return new MyGameView(mContext);
    }

    @Override
    public void onImageLoaded(LiteImageCacheObject obj) {
        mImageObj = obj;
        try {
            if (mImageObj.getBitmap() != null) {
                ((MyGameView) mItemView).updateIcon(new BitmapDrawable(mImageObj.getBitmap()));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
